/**
 * 与位置相关的类
 */
define([
    'require',
], function(require) {
    'use strict';
    /**
     * 位置类,实时表示鼠标在屏幕上的位置.在WSG上的坐标以及相机的坐标
     * @class
     * @param {*} viewer 
     * @param {Function} callback Position有变化时调用的回调
     */
    function Position(viewer, callback){
        if(typeof viewer === 'undefined'){
            throw new Error('viewer is required!');
        }
        if(typeof callback !== 'function'){
            callback = function(){}
        }
        this._viewer = viewer;
        this._mousePositionOnScreen = null;     // 鼠标指针在屏幕上坐标
        this._mousePositionOnWSG = null;        // 鼠标指针在世界坐标系坐标
        this._cameraPosition = null;            // 相机坐标
        this._callback = callback;
        this._init();
    }
    Object.defineProperties(Position.prototype, {
        mousePositionOnScreen : {
            get : function(){
                return this._mousePositionOnScreen;
            }
        },
        mousePositionOnWSG : {
            get : function(){
                return this._mousePositionOnWSG;
            }
        },
        cameraPosition : {
            get : function(){
                return this._cameraPosition;
            }
        }
    })
    Position.prototype._init = function(){
        var scene = this._viewer.scene;
        // 获取当前三维场景对应的椭球体
        var ellipsoid = scene.globe.ellipsoid;
        // 获取场景对应的句柄
        var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);

        // 设置初始坐标
        this._mousePositionOnScreen = new Cesium.Cartesian2(0,0);
        this._mousePositionOnWSG = new Cesium.Cartesian3(0,0,0);
        this._cameraPosition = this._viewer.camera.position;

        var that = this;
        // 监听鼠标事件
        handler.setInputAction(function(movement){
            var viewer = that._viewer;
            // 设置鼠标在屏幕上坐标
            that._mousePositionOnScreen = movement.endPosition;

            // 鼠标的二维坐标转换为对应椭球体三维坐标
            var cartesian = scene.pickPosition(movement.endPosition);

            if (cartesian) {
                // 笛卡尔坐标转换为地理坐标
                var cartographic = ellipsoid.cartesianToCartographic(cartesian);
                // 弧度转为度的十进制度表示
                var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
                var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
                var height = cartographic.height;

                that._mousePositionOnWSG = new Cesium.Cartesian3(longitudeString, latitudeString, height); 
            }
            that._callback();
        },Cesium.ScreenSpaceEventType.MOUSE_MOVE);

        // 监听滚轮事件
        handler.setInputAction(function(wheel){
            var height = that._viewer.camera.positionCartographic.height;
            that._cameraPosition.z = height;
            that._callback();
        },Cesium.ScreenSpaceEventType.WHEEL)

        // 监听相机移动结束
        // 监听每一帧获取相机位置
        this._viewer.camera.changed.addEventListener(function(){
            var viewer = that._viewer;
            //that._cameraPosition = that._viewer.camera.position;
            var cartesian = viewer.camera.pickEllipsoid(that._viewer.camera.position, ellipsoid);
            if (cartesian) {
                // 笛卡尔坐标转换为地理坐标
                var cartographic = ellipsoid.cartesianToCartographic(cartesian);
                // 弧度转为度的十进制度表示
                var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
                var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
                var height = that._viewer.camera.position.z;

                that._cameraPosition = new Cesium.Cartesian3(longitudeString, latitudeString, height); 
            }
            that._callback();
        })
    }
    return Position;
});